package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.constant.HardwareStateEnum;
import com.workcell.workdata.xthings.entity.BaseFlowmeter;
import com.workcell.workdata.xthings.entity.CodeChainnum;
import com.workcell.workdata.xthings.entity.CodeFlowmeter;
import com.workcell.workdata.xthings.mapper.CodeFlowmeterMapper;
import com.workcell.workdata.xthings.service.BaseFlowmeterService;
import com.workcell.workdata.xthings.service.CodeFlowmeterService;
import com.workcell.workdata.xthings.service.factory.CodeBaseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 流量计二维码
 *
 * @author 廖博文
 * @date 2022-11-28 10:15:36
 */
@Service
public class CodeFlowmeterServiceImpl extends ServiceImpl<CodeFlowmeterMapper, CodeFlowmeter> implements CodeFlowmeterService, CodeBaseService {

	@Resource
	private BaseFlowmeterService baseFlowmeterService;

	/**
	 * 保存二维码基本快照信息
	 * @param codeChainnum
	 * @return
	 */
	@Override
	public Boolean saveCodeBaseInfo(CodeChainnum codeChainnum) {
		// 查询硬件信息
		LambdaQueryWrapper<BaseFlowmeter> baseFlowmeterCondition = new QueryWrapper<BaseFlowmeter>().lambda()
				.eq(BaseFlowmeter::getFmId,codeChainnum.getBaseId());
		BaseFlowmeter baseFlowmeter = baseFlowmeterService.getOne(baseFlowmeterCondition);
		if (ObjectUtil.isEmpty(baseFlowmeter)){
			return false;
		}
		// 添加二维码快照信息
		CodeFlowmeter saveBean = new CodeFlowmeter();
		BeanUtil.copyProperties(baseFlowmeter,saveBean, "createBy","createTime","updateBy","updateTime");
		saveBean.setCodeId(codeChainnum.getCodeId());
		saveBean.setChainNum(codeChainnum.getChainNum());
		saveBean.setDeviceNo(codeChainnum.getDeviceNo());
		return this.save(saveBean);
	}

	/**
	 * 获取硬件清单
	 * @return
	 */
	@Override
	public List<JSONObject> getHardwareList() {
		LambdaQueryWrapper<BaseFlowmeter> lambdaQueryWrapper = Wrappers.lambdaQuery(BaseFlowmeter.class);
		lambdaQueryWrapper.eq(BaseFlowmeter::getState, HardwareStateEnum.INUSE.getCode());
		return baseFlowmeterService.list(lambdaQueryWrapper).stream().map(i -> {
			JSONObject resultBean = new JSONObject();
			resultBean.put("baseId",i.getFmId());
			resultBean.put("manufacturer",i.getManufacturer());
			resultBean.put("hardwareName",i.getFmName());
			return resultBean;
		}).collect(Collectors.toList());
	}

	/**
	 * 查询硬件基本信息
	 * @param baseIds
	 * @return
	 */
	@Override
	public Object getHardwareBase(String baseIds) {
		List<String> baseList = Arrays.asList(baseIds.split(","));
		LambdaQueryWrapper<BaseFlowmeter> baseFlowmeterCondition = new QueryWrapper<BaseFlowmeter>().lambda()
				.in(CollectionUtil.isNotEmpty(baseList),BaseFlowmeter::getFmId,baseList);
		List<BaseFlowmeter> baseFlowmeterList = baseFlowmeterService.list(baseFlowmeterCondition);
		return baseFlowmeterList;
	}

	/**
	 * 查询硬件快照信息
	 * @param codeId
	 * @return
	 */
	@Override
	public Object getHardwareInfo(String codeId) {
		return this.getById(codeId);
	}

}
